package com.finchina.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Paint;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.widget.ImageView;

/**
 * 作者：黄赛赛 on 2015/9/8 14:14
 *http://blog.csdn.net/lmj623565791/article/details/24300125
 * @version V1.0
 * @Description: ${todo}()
 */
public class CustomImageView extends ImageView {
    final String TAG = "CustomImageView";
    Bitmap mImage;
    int mImageScale;
    /**
     * 文本
     */
    private String mTitleText;
    /**
     * 文本的颜色
     */
    private int mTitleTextColor;
    /**
     * 文本的大小
     */
    private int mTitleTextSize;


    Paint mPaint;
    Rect mTextBound, rect;


    final int FILLXY = 0;
    final int CENTER = 1;
    public CustomImageView(Context context) {
//        super(context);
        this(context,null);
    }


    public CustomImageView(Context context, AttributeSet attrs) {
//        super(context, attrs);
            this(context,attrs,0);
    }


    public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageView, defStyleAttr,0);

        mImage = BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.CustomImageView_image, R.mipmap.ic_launcher));
        mImageScale = ta.getInt(R.styleable.CustomImageView_scaleType, 0);

        mTitleText = ta.getString(R.styleable.CustomImageView_imageTitleText);
        mTitleTextColor = ta.getColor(R.styleable.CustomImageView_imageTitleColor, Color.BLACK);
        mTitleTextSize = ta.getDimensionPixelSize(R.styleable.CustomImageView_imageTitleSize,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
        ta.recycle();

        mPaint = new Paint();
        rect = new Rect();
        mTextBound = new Rect();
        mPaint.setTextSize(mTitleTextSize);
        // 计算了描绘字体需要的范围
        mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mTextBound);

    }

    /*    EXACTLY：一般是设置了明确的值或者是MATCH_PARENT

        AT_MOST：表示子布局限制在一个最大值内，一般为WARP_CONTENT

        UNSPECIFIED：表示子布局想要多大就多大，很少使用*/
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        Log.e(TAG, "widthSize =" + widthSize);
        Log.e(TAG, "heightSize =" + heightSize);

        Log.e(TAG, "mImage.getWidth() =" + mImage.getWidth());
        Log.e(TAG, "mImage.getHeight() =" + mImage.getHeight());


        Log.e(TAG, "mTextBound.getWidth() =" + mTextBound.width());
        Log.e(TAG, "mTextBound.getHeight() =" + mTextBound.height());


        int width =0, height = 0;
        if (widthMode == MeasureSpec.EXACTLY){// match_parent , accurate
            width = widthSize;
        }else {
            // 由图片决定的宽
            int desireByImg = getPaddingLeft() + getPaddingRight() + mImage.getWidth();
            // 由字体决定的宽
            int desireByText = getPaddingLeft() + getPaddingRight() + mTextBound.width();
            if (widthMode == MeasureSpec.AT_MOST){//wrap_content
                int desire = Math.max(desireByImg, desireByText);
                width = Math.min(desire, widthSize);
            }
        }


        if (heightMode == MeasureSpec.EXACTLY){
            height = heightSize;
        }else {
            int desire = getPaddingTop() + getPaddingBottom() + mTextBound.height() + mImage.getHeight();
            if (heightMode == MeasureSpec.AT_MOST){
                height = Math.min(desire, heightSize);
            }
        }
        Log.e(TAG, "width =" + width);
        Log.e(TAG, "height =" + height);

        setMeasuredDimension(width,height);

    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);


        int width =  getMeasuredWidth();
        int height = getMeasuredHeight();
        /**
         * 边框
         */
        mPaint.setStrokeWidth(4);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.CYAN);
        canvas.drawRect(0, 0, width, height, mPaint);

        rect.left = getPaddingLeft();
        rect.right = getWidth() - getPaddingRight();
        rect.top = getPaddingTop();
        rect.bottom = getHeight() - getPaddingBottom();

        mPaint.setColor(mTitleTextColor);
        mPaint.setStyle(Paint.Style.FILL);

        /**
         * 当前设置的宽度小于字体需要的宽度，将字体改为xxx...
         */
        if (mTextBound.width() > getWidth()){
            TextPaint paint = new TextPaint(mPaint);
            String msg = TextUtils.ellipsize(mTitleText, paint, (float) width - getPaddingLeft() - getPaddingRight(),
                    TextUtils.TruncateAt.END).toString();
            canvas.drawText(msg, getPaddingLeft(), height - getPaddingBottom(), mPaint);
        } else {
            //正常情况，将字体居中
            canvas.drawText(mTitleText, (width - mTextBound.width())/2, height - getPaddingBottom(),mPaint);
        }


        //取消使用掉的快
        rect.bottom -= mTextBound.height();
        if (mImageScale == FILLXY){
//            canvas.drawBitmap(mImage, null,rect, mPaint);
        } else {//计算居中的矩形范围
            rect.left = (width - mImage.getWidth()) /2;
            rect.right = (width + mImage.getWidth()) /2;
            rect.top = (height - mTextBound.height()  - mImage.getHeight()) /2;
            rect.bottom = (height - mTextBound.height()  + mImage.getHeight()) /2;


        }
        canvas.drawBitmap(mImage, null,rect, mPaint);

    }



}
